import {Input, message, Modal} from 'antd'
import {forwardRef, useCallback, useImperativeHandle, useState} from 'react'
import {receivedModalType} from '@/type/receivedType'
import {orderReceivedSave} from '@/api'

export const ReceivedModal = forwardRef((props, ref) => {
	const [open, setOpen] = useState<boolean>(false)
	const [remark, setValue] = useState<string>('')
	const [data, setData] = useState<receivedModalType>({
		product_id: ""
	})

	const modalInit = (data: receivedModalType) => {
		setOpen(true)
		setData(data)
		setValue("")
	}

	const onCancel = useCallback(() => {
		setOpen(false)
	}, [])

	const onOk = useCallback(() => {
		orderReceivedSave({
			product_id: data.product_id,
			remark
		}).then(res => {
			if (res.code === 200) {
				message.success(res.message)
				setOpen(false)
			}
		})
	}, [remark, data] )


	useImperativeHandle(ref, () => {
		return {
			modalInit
		}
	})
	return (
		<Modal open={open} title={'评价'} onCancel={onCancel} onOk={onOk}>
			<Input.TextArea
				placeholder={'请输入评价内容'}
				value={remark}
				onChange={(v) => setValue(v.target.value)}
			/>
		</Modal>
	)
})
